package widget.common.utils
{
	import flash.geom.Point;

	public class MathUtils{
		
		public function MathUtils()
		{
			throw new Error("can not new!!!");
		}
		
		/**
		 * @function	distance
		 * @param	x1	点1的横坐标
		 * @param	y1	点1的纵坐标
		 * @param	x2	点2的横坐标
		 * @param	y2	点2的纵坐标
		 * @return		返回两点间距
		 */
		public static function distance(x1:Number,y1:Number,x2:Number,y2:Number):Number {
			var disX = x1 - x2;
			var disY = y1 - y2;
			
			/*disX = decLimit(disX,2);
			disY = decLimit(disY,2);*/
			
			return Math.sqrt(disX * disX + disY * disY);
		}
		/**
		 * @function 	angle
		 * 
		 * @param	dx
		 * @param	dy
		 * @param	radian		是否返回弧度,默认为false
		 * @return	a
		 * 
		 * @usage	根据dx,dy计算两条线段夹角的角度,并返回
		 */
		public static function angle(dx:Number, dy:Number, radian:Boolean = false):Number {
			var a:Number = 0;
			if (dx != 0) {
				dx = decLimit(dx, 4);
				if (radian) {
					a = Math.atan(dy / dx);
				}else {
					a = Math.atan(dy / dx) * 180 / Math.PI;
				}
			}
			return a;			
		}
		public static function decLimit(num:Number, dec:int):Number {
			var tem = Math.pow(10, dec);
			var result:Number=int(num * tem) / tem;
			return result;
		}
		/**
		 * @function 	getBorderPoint
		 * 
		 * @param	mousePoint		舞台中鼠标的坐标
		 * @param	area			鼠标划过的角
		 * @param	fWidth			FlipBook的宽度
		 * @param	fHeight			FlipBook的高度
		 * @return	[Hpoint,Vpoint]
		 * 
		 * @usage	将舞台的鼠标坐标,转换为有效的鼠标坐标,并返回
		 */
		public static function getBorderPoint(mousePoint:Point, area:Number, fWidth:Number, fHeight:Number):Array {			
			var mx = mousePoint.x;
			var my = mousePoint.y;
			
			var hx:Number;
			var hy:Number;
			var vx:Number;
			var vy:Number;
			var forthx:Number;
			var forthy:Number;
			
			var angleMtoF:Number;//鼠标与点ForthPoint的角度
			
			switch(area) {
				case 0:
					//计算Hpoint的坐标值
					hx = my * my/(2*mx)+mx/2;
					if (hx > fWidth/2) hx=fWidth/2;//这里保证Hpoint的值不会越过中轴线					
					hy = 0;
					//计算Vpoint的坐标值
					vx = 0;
					vy = mx * mx/(2*my)+my/2;
					
					//如果垂Vpoint的y坐标<0或>Book的高度,重新计算Vpoint和第四个点Fpoint.
					if (vy > fHeight || vy < 0) {
						
						angleMtoF = angle(mx - vx, my - vy, true);	
						if (vy < 0) {
							forthx = mx + Math.cos(angleMtoF) * fHeight;
							forthy = my + Math.sin(angleMtoF) * fHeight;
						}else {
							forthx = mx - Math.cos(angleMtoF) * fHeight;
							forthy = my - Math.sin(angleMtoF) * fHeight;
						}		
						
						vx = my * my/2/mx - my/mx*fHeight+mx/2;						
						vy = fHeight;
					}else {
						forthx = -100;
					}
					
					
					break;
				case 1:
					hx = (my * my + mx * mx - fWidth * fWidth) / (2 * (mx - fWidth));
					if (hx < fWidth/2) hx=fWidth/2;
					hy = 0;
					
					vx = fWidth;
					vy = (mx - fWidth)*(mx - fWidth)/2 / my+my/2;
					
					if (vy >= fHeight || vy <= 0) {						
						angleMtoF = angle(mx - vx, my - vy, true);	
						if (vy <= 0) {
							forthx = mx - Math.cos(angleMtoF) * fHeight;
							forthy = my - Math.sin(angleMtoF) * fHeight;
						}else {
							forthx = mx + Math.cos(angleMtoF) * fHeight;
							forthy = my + Math.sin(angleMtoF) * fHeight;
						}
						
						vx = (mx + fWidth)/2-my*(fHeight-my/2) / (mx - fWidth);					
						vy = fHeight;
					}else {
						forthx = -100;
					}
					
					break;
				case 2:
					hx = (my - fHeight) * (my - fHeight)/2 / mx + mx / 2;
					if (hx > fWidth/2) hx=fWidth/2;
					hy = fHeight;
					
					vx = 0;
					vy = (my * my + mx * mx - fHeight * fHeight) / (2 * (my - fHeight));
					
					if (vy > fHeight || vy < 0) {						
						angleMtoF = angle(mx - vx, my - vy, true);	
						if (vy < 0) {
							forthx = mx - Math.cos(angleMtoF) * fHeight;
							forthy = my - Math.sin(angleMtoF) * fHeight;
						}else {
							forthx = mx + Math.cos(angleMtoF) * fHeight;
							forthy = my + Math.sin(angleMtoF) * fHeight;
						}
						
						vx = mx/2-(my-fHeight)*(-(my+fHeight)/2) / mx;					
						vy = 0;
					}else {
						forthx = -100;
					}
					
					break;
				case 3:
					hx = (mx * mx + (my - fHeight) * (my - fHeight) - fWidth * fWidth) / (2 * (mx - fWidth));
					hy = fHeight;
					if (hx < fWidth/2) hx=fWidth/2;
					
					vx = fWidth;
					vy = (my * my + (mx - fWidth) * (mx - fWidth) - fHeight * fHeight) / (2 * (my - fHeight));	
					
					if (vy > fHeight || vy < 0) {
						
						angleMtoF = angle(mx - vx, my - vy, true);	
						if (vy < 0) {
							forthx = mx + Math.cos(angleMtoF) * fHeight;
							forthy = my + Math.sin(angleMtoF) * fHeight;
						}else {
							forthx = mx - Math.cos(angleMtoF) * fHeight;
							forthy = my - Math.sin(angleMtoF) * fHeight;
						}						
						
						vx = (mx + fWidth)/2-(my-fHeight)*(-(my+fHeight)/2) / (mx - fWidth);							
						vy = 0;
					}else {
						forthx = -100;
					}					
					break;
			}
			
			return [new Point(hx, hy), new Point(vx, vy), new Point(forthx, forthy)];
			
		}
		/**
		 * @function	getMousePoint
		 * 
		 * @param	mouse		实际鼠标的坐标
		 * @param	tPoint		中轴上端坐标
		 * @param	bPoint		中轴低端坐标
		 * @param	radius		有效区域半径
		 * @return	point		返回有效的鼠标坐标
		 * 
		 * @usage	获取有效的鼠标位置
		 */
		public static function getMousePoint(mPoint:Point,tPoint:Point,radius:Number):Point {
			
			var mx :Number = mPoint.x;
			var my :Number = mPoint.y;
			
			if (mx < 0) mx = 0;
			if (mx > tPoint.x * 2) mx = tPoint.x * 2;
			
			var newx:Number=mx;
			var newy:Number=my;
			
			var dis:Number = distance(mx, my, tPoint.x, tPoint.y);//鼠标到target点的距离
			
			if (dis > radius) {
				var scale = radius / dis;
				
				newx = tPoint.x + (mx - tPoint.x) * scale;
				newy = tPoint.y + (my - tPoint.y) * scale;				
			}
			
			return new Point(newx, newy);
		}
	}
}